
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Quang
 */
public class FileManager {

    private String path;
    private File file;

    /**
     * @return the path
     */
    public String getPath() {
        return path;
    }

    /**
     * @param path the path to set
     */
    public void setPath(String path) {
        this.path = path;
    }

    public FileManager(String p) {
        path = p;
        file = new File(path);
    }

    public FileManager() {
    }

    public Boolean CopyFile(String pathSource, String pathDestination) {
        try {
            // doc du lieu tu file nguon
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pathSource));
            file = new File(pathSource);

            pathDestination += file.getName();
            // ghi du lieu ra file dich
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(pathDestination));

            while (true) {
                int dam = bis.read();
                if (dam == -1) {
                    break;
                }
                bos.write(dam);

            }
            bos.flush();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public void Delete(String fileName) {
   
    
    File f = new File(fileName);

    
    if (!f.exists()){
      throw new IllegalArgumentException(
          "Xoa: Khong ton tai file: " + fileName);
    }

    if (!f.canWrite())
      throw new IllegalArgumentException("Xoa: Khong xoa dc "
          + fileName);

    // If it is a directory, make sure it is empty
    if (f.isDirectory()) {
      String[] files = f.list();
      if (files.length > 0)
        throw new IllegalArgumentException(
            "Xoa: thu muc khong rong: " + fileName);
    }

    // Attempt to delete it
    boolean success = f.delete();

    if (!success)
      throw new IllegalArgumentException("Xoa: that bai!");
  }

    public Boolean Rename(String pathSource, String newName) {
        File fSource = new File(pathSource);
        newName = fSource.getParent() + newName;
        return fSource.renameTo(new File(newName));

    }

    public Boolean ViewFile(String pathSource) {
        try {
            // doc du lieu tu file nguon
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pathSource));


            // ghi du lieu ra file dich
            BufferedOutputStream bos = new BufferedOutputStream(System.out);

            while (true) {
                int dam = bis.read();
                if (dam == -1) {
                    break;
                }
                bos.write(dam);

            }
            bos.flush();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public void UnZip(String sourcefile) {
        OutputStream out = null;
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        try {
            if (!sourcefile.endsWith(".zip")) {
                System.out.println("Invalid file name!");
                System.exit(0);
            } else if (!new File(sourcefile).exists()) {
                System.out.println("File not exist!");
                System.exit(0);
            }
            ZipInputStream in = new ZipInputStream(new FileInputStream(sourcefile));
            ZipFile zf = new ZipFile(sourcefile);
            int a = 0;
            for (Enumeration em = zf.entries(); em.hasMoreElements();) {
                String targetfile = em.nextElement().toString();
                ZipEntry ze = in.getNextEntry();
                out = new FileOutputStream(targetfile);
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                a = a + 1;
            }
            if (a > 0) {
                System.out.println("Files unzipped.");
            }
            out.close();
            in.close();
        } catch (IOException e) {
            System.out.println("Error: Operation failed!");
            System.exit(0);
        }
    }
}
